package com.anolesoft.twelvePlan.mapper;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.twelvePlan.entity.MesGyInfoDO;
import com.anolesoft.twelvePlan.entity.MesWxInfoDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @author zyq
 * @date 2022/2/22
 */
@Mapper
public interface MesWXInfoMapper extends BaseMapper<MesWxInfoDO> {
    @SelectProvider(type = createSQL.class, method = "queryWXInfoSQL")
    List<MesWxInfoDO> queryWXInfo(@Param("MesWxInfoDO") MesWxInfoDO MesWxInfoDO, Page<MesWxInfoDO> page);
    public static class createSQL {
        public static String queryWXInfoSQL(@Param("MesWxInfoDO") MesWxInfoDO MesWxInfoDO, Page<MesWxInfoDO> page){
            return new SQL(){
                {
                    StringBuffer sql = new StringBuffer();
                    sql.append(" a.objuid, a.jx, a.partid, a.name, a.material, a.ljsize, a.pgh, a.djnum, a.cgnum, ");
                    sql.append(" a.wxprocess, a.neworcontinue, a.selfflag, a.category, a.wxfactory, a.remarks1, a.remarks, ");
                    sql.append(" a.deleteflag, a.createuid, a.modifyuid, a.deleteuid, a.factory_id ");
                    sql.append(" from mes_wx_info a where a.deleteflag = 0 ");
                    sql.append(" and a.factory_id = #{MesWxInfoDO.factoryId, jdbcType=VARCHAR}");
                    if (!StrUtil.isEmptyOrUndefined(MesWxInfoDO.getJx())){
                        sql.append(" and a.jx = trim(upper(#{MesWxInfoDO.jx, jdbcType=VARCHAR})) ");
                    }
                    if (!StrUtil.isEmptyOrUndefined(MesWxInfoDO.getPartid())){
                        sql.append(" and a.partid = trim(upper(#{MesWxInfoDO.partid, jdbcType=VARCHAR})) ");
                    }
                    if (!StrUtil.isEmptyOrUndefined(MesWxInfoDO.getCategory())){
                        sql.append(" and a.category = #{MesWxInfoDO.category, jdbcType=VARCHAR} ");
                    }
                    if (!StrUtil.isEmptyOrUndefined(MesWxInfoDO.getNeworcontinue())){
                        sql.append(" and a.neworcontinue = #{MesWxInfoDO.neworcontinue, jdbcType=VARCHAR} ");
                    }
                    //sql.append(" order by a.createtime desc");
                    SELECT(sql.toString());
                    System.out.println(">>>>queryWXInfoSQL:" + sql.toString());
                }
            }.toString();
        }
    }

    @SelectProvider(type = createSQL2.class, method = "repeatWXInfoSQL")
    int repeatWXInfo(@Param("MesWxInfoDO") MesWxInfoDO MesWxInfoDO);
    public static class createSQL2 {
        public static String repeatWXInfoSQL(@Param("MesWxInfoDO") MesWxInfoDO MesWxInfoDO){
            return new SQL(){
                {
                    StringBuffer sql = new StringBuffer();
                    sql.append(" count(*) from mes_wx_info a where a.jx = trim(upper(#{MesWxInfoDO.jx, jdbcType=VARCHAR})) ");
                    sql.append(" and a.partid = trim(upper(#{MesWxInfoDO.partid, jdbcType=VARCHAR})) ");
                    sql.append(" and a.pgh = #{MesWxInfoDO.pgh, jdbcType=VARCHAR} ");
                    sql.append(" and a.deleteflag = 0");
                    if (!StrUtil.isEmptyOrUndefined(MesWxInfoDO.getObjuid())){
                        sql.append(" and a.objuid != #{MesWxInfoDO.objuid, jdbcType=VARCHAR}");
                    }
                    SELECT(sql.toString());
                    System.out.println(">>>>repeatWXInfoSQL:" + sql.toString());
                }
            }.toString();
        }
    }

    @Insert("insert into mes_wx_info(jx, partid, name, material, ljsize, pgh, djnum, cgnum, " +
            " wxprocess, neworcontinue, selfflag, category, wxfactory, remarks1, remarks, " +
            " createuid, createtime, factory_id) values (" +
            " trim(upper(#{MesWxInfoDO.jx, jdbcType=VARCHAR})), trim(upper(#{MesWxInfoDO.partid, jdbcType=VARCHAR})), #{MesWxInfoDO.name, jdbcType=VARCHAR}, " +
            " #{MesWxInfoDO.material, jdbcType=VARCHAR}, #{MesWxInfoDO.ljsize, jdbcType=VARCHAR}, #{MesWxInfoDO.pgh, jdbcType=VARCHAR}, " +
            " #{MesWxInfoDO.djnum, jdbcType=INTEGER}, #{MesWxInfoDO.cgnum, jdbcType=INTEGER}, #{MesWxInfoDO.wxprocess, jdbcType=VARCHAR}, " +
            " #{MesWxInfoDO.neworcontinue, jdbcType=VARCHAR}, #{MesWxInfoDO.selfflag, jdbcType=VARCHAR}, #{MesWxInfoDO.category, jdbcType=VARCHAR}, " +
            " #{MesWxInfoDO.wxfactory, jdbcType=VARCHAR}, #{MesWxInfoDO.remarks1, jdbcType=VARCHAR}, #{MesWxInfoDO.remarks, jdbcType=VARCHAR}, " +
            " #{MesWxInfoDO.createuid, jdbcType=VARCHAR}, sysdate, #{MesWxInfoDO.factoryId, jdbcType=VARCHAR})")
    int insertWXInfo(@Param("MesWxInfoDO") MesWxInfoDO MesWxInfoDO);

    @Update("update mes_wx_info a set " +
            " a.jx = trim(upper(#{MesWxInfoDO.jx, jdbcType=VARCHAR}))," +
            " a.partid = trim(upper(#{MesWxInfoDO.partid, jdbcType=VARCHAR}))," +
            " a.name = #{MesWxInfoDO.name, jdbcType=VARCHAR}," +
            " a.material = #{MesWxInfoDO.material, jdbcType=VARCHAR}," +
            " a.ljsize = #{MesWxInfoDO.ljsize, jdbcType=VARCHAR}," +
            " a.pgh = #{MesWxInfoDO.pgh, jdbcType=VARCHAR}," +
            " a.djnum = #{MesWxInfoDO.djnum, jdbcType=INTEGER}," +
            " a.cgnum = #{MesWxInfoDO.cgnum, jdbcType=INTEGER}," +
            " a.wxprocess = #{MesWxInfoDO.wxprocess, jdbcType=VARCHAR}," +
            " a.neworcontinue = #{MesWxInfoDO.neworcontinue, jdbcType=VARCHAR}," +
            " a.selfflag = #{MesWxInfoDO.selfflag, jdbcType=VARCHAR}," +
            " a.category = #{MesWxInfoDO.category, jdbcType=VARCHAR}," +
            " a.wxfactory = #{MesWxInfoDO.wxfactory, jdbcType=VARCHAR}," +
            " a.remarks1 = #{MesWxInfoDO.remarks1, jdbcType=VARCHAR}," +
            " a.remarks = #{MesWxInfoDO.remarks, jdbcType=VARCHAR}," +
            " a.modifyuid = #{MesWxInfoDO.modifyuid, jdbcType=VARCHAR}," +
            " a.modifytime = sysdate" +
            " where a.objuid = #{MesWxInfoDO.objuid, jdbcType=VARCHAR}")
    int updateWXInfo(@Param("MesWxInfoDO") MesWxInfoDO MesWxInfoDO);

    @Update("update mes_wx_info a set " +
            " a.deleteflag = 1, " +
            " a.deleteuid = #{MesWxInfoDO.deleteuid, jdbcType=VARCHAR}," +
            " a.deletetime = sysdate" +
            " where a.objuid = #{MesWxInfoDO.objuid, jdbcType=VARCHAR}")
    int deleteWXInfo(@Param("MesWxInfoDO") MesWxInfoDO MesWxInfoDO);

    @UpdateProvider(type = createSQL4.class, method = "deleteSelectSQL")
    int deleteSelect(@Param("MesWxInfoDO") MesWxInfoDO MesWxInfoDO);
    public static class createSQL4 {
        public static String deleteSelectSQL(@Param("MesWxInfoDO") MesWxInfoDO MesWxInfoDO){
            return new SQL(){
                {
                    StringBuffer sql = new StringBuffer();
                    sql.append(" mes_wx_info a set a.deleteflag = 1, a.deleteuid = #{MesWxInfoDO.deleteuid},");
                    sql.append(" a.deletetime = sysdate where a.deleteflag = 0");
                    sql.append(" and a.factory_id = #{MesWxInfoDO.factoryId} ");
                    if (!StrUtil.isEmptyOrUndefined(MesWxInfoDO.getJx())){
                        sql.append(" and a.jx = trim(upper(#{MesWxInfoDO.jx, jdbcType=VARCHAR})) ");
                    }
                    if (!StrUtil.isEmptyOrUndefined(MesWxInfoDO.getPartid())){
                        sql.append(" and a.partid = trim(upper(#{MesWxInfoDO.partid, jdbcType=VARCHAR})) ");
                    }
                    if (!StrUtil.isEmptyOrUndefined(MesWxInfoDO.getCategory())){
                        sql.append(" and a.category = #{MesWxInfoDO.category, jdbcType=VARCHAR} ");
                    }
                    if (!StrUtil.isEmptyOrUndefined(MesWxInfoDO.getNeworcontinue())){
                        sql.append(" and a.neworcontinue = #{MesWxInfoDO.neworcontinue, jdbcType=VARCHAR} ");
                    }
                    UPDATE(sql.toString());
                    System.out.println(">>>>deleteSelectSQL:" + sql.toString());
                }
            }.toString();
        }
    }

    @SelectProvider(type = createSQL3.class, method = "exportWXInfoSQL")
    int exportWXInfo(@Param("MesWxInfoDO") MesWxInfoDO MesWxInfoDO);
    public static class createSQL3 {
        public static String exportWXInfoSQL(@Param("MesWxInfoDO") MesWxInfoDO MesWxInfoDO){
            return new SQL(){
                {
                    StringBuffer sql = new StringBuffer();
                    sql.append(" count(*) from mes_wx_info a where a.deleteflag = 0 ");
                    sql.append(" and a.factory_id = #{MesWxInfoDO.factoryId, jdbcType=VARCHAR}");
                    if (!StrUtil.isEmptyOrUndefined(MesWxInfoDO.getJx())){
                        sql.append(" and a.jx = trim(upper(#{MesWxInfoDO.jx, jdbcType=VARCHAR})) ");
                    }
                    if (!StrUtil.isEmptyOrUndefined(MesWxInfoDO.getPartid())){
                        sql.append(" and a.partid = trim(upper(#{MesWxInfoDO.partid, jdbcType=VARCHAR})) ");
                    }
                    if (!StrUtil.isEmptyOrUndefined(MesWxInfoDO.getCategory())){
                        sql.append(" and a.category = #{MesWxInfoDO.category, jdbcType=VARCHAR} ");
                    }
                    if (!StrUtil.isEmptyOrUndefined(MesWxInfoDO.getNeworcontinue())){
                        sql.append(" and a.neworcontinue = #{MesWxInfoDO.neworcontinue, jdbcType=VARCHAR} ");
                    }
                    SELECT(sql.toString());
                    System.out.println(">>>>exportWXInfoSQL:" + sql.toString());
                }
            }.toString();
        }
    }
}

